using Google.ProtocolBuffers;
using micromsgBackground;
using MicroMsgBackground.Common.Utils;
using MicroMsgBackground.Manager;
using MicroMsgBackground.Protocol;
using System;
namespace MicroMsgBackground.Network
{
	public class AuthPack
	{
		public static SessionPack makeAutoAuthPack(int scene)
		{
			AuthRequest authRequest = AuthPack.makeAuthRequest(scene);
			if (authRequest == null)
			{
				return null;
			}
			SessionPack sessionPack = new SessionPack();
			sessionPack.mCmdID = 1;
			sessionPack.mRequestObject = authRequest;
			sessionPack.mProcRequestToByteArray += new RequestToByteArrayDelegate(AuthPack.authRequestToByteArray);
			sessionPack.mCompleted += new SessionPackCompletedDelegate(AuthPack.onAutoAuthCompleted);
			return sessionPack;
		}
		private static void onAutoAuthCompleted(object sender, PackEventArgs e)
		{
			if (!e.isSuccess())
			{
				Log.smethod_1("Network", "auto auth failed .");
				return;
			}
			SessionPack sessionPack = sender as SessionPack;
			AuthResponse authResponse = (AuthResponse)sessionPack.mResponseObject;
			RetConst ret = (RetConst)authResponse.BaseResponse.Ret;
			if (ret != RetConst.MM_OK && ret != RetConst.MM_ERR_CRITICALUPDATE)
			{
				if (ret != RetConst.MM_ERR_RECOMMENDEDUPDATE)
				{
					if (ret != RetConst.MM_ERR_NEED_VERIFY)
					{
						Log.smethod_1("Network", "auto auth failed, result = " + ret);
						return;
					}
					if (authResponse.ImgBuf != null && authResponse.ImgSid != null)
					{
						Log.smethod_1("Network", "auto auth failed, need verify,  sid = " + new VerifyCodeArgs
						{
							mImageSid = authResponse.ImgSid.String,
							mImageBuf = authResponse.ImgBuf.Buffer.ToByteArray()
						}.mImageSid);
						return;
					}
					Log.smethod_1("Network", "NEED_VERIFY_USER, but ImgSid or ImgBuf is null");
					return;
				}
			}
			Log.smethod_4("Network", "auto auth success. ");
			AccountBackground curAccount = AccountMgr.getCurAccount();
			curAccount.bytesSessionkey = authResponse.SessionKey.ToByteArray();
			curAccount.nUin = authResponse.Uin;
			AccountMgr.saveBackgroundAccout(curAccount);
		}
		public static void updateAccountInfoFromAuth(object request)
		{
			AuthRequest authRequest = request as AuthRequest;
			SessionPackMgr.getAccount().setAuthInfo(authRequest.UserName.String, authRequest.Pwd.String, authRequest.Pwd2);
		}
		private static AuthRequest makeAuthRequest(int scene)
		{
			if (!SessionPackMgr.getAccount().isValid())
			{
				return null;
			}
			BaseRequest baseRequest = BaseRequest.DefaultInstance;
			BaseRequest.Builder builder = BaseRequest.CreateBuilder();
			builder.SessionKey = ByteString.CopyFrom(SessionPackMgr.getSessionKeyEx());
			builder.Uin = (uint)SessionPackMgr.getAccount().getUin();
			builder.DeviceID = ByteString.CopyFromUtf8(Util.getDeviceUniqueId());
			builder.ClientVersion = (int)ConstantsProtocol.CLIENT_VERSION;
			builder.DeviceType = ByteString.CopyFrom(Util.toFixLenString(ConstantsProtocol.DEVICE_TYPE, 132));
			builder.Scene = (uint)scene;
			baseRequest = builder.Build();
			AuthRequest arg_82_0 = AuthRequest.DefaultInstance;
			AuthRequest.Builder builder2 = AuthRequest.CreateBuilder();
			builder2.BaseRequest = baseRequest;
			builder2.UserName = Util.toSKString(SessionPackMgr.getAccount().getUsername());
			builder2.Pwd = Util.toSKString(SessionPackMgr.getAccount().getPassword());
			builder2.ImgSid = Util.toSKString("");
			builder2.ImgCode = Util.toSKString("");
			builder2.Language = AccountMgr.GetCurrentLanguage();
			return builder2.Build();
		}
		private static byte[] authRequestToByteArray(object object_0)
		{
			AuthRequest authRequest = object_0 as AuthRequest;
			return authRequest.ToByteArray();
		}
		public static object onParserAuthPack(SessionPack sessionPack)
		{
			AuthResponse authResponse = AuthResponse.ParseFrom(sessionPack.mResponseBuffer);
			if (authResponse == null)
			{
				Log.smethod_1("Network", "auth parse failed. ");
				return null;
			}
			int uin = (int)authResponse.Uin;
			RetConst ret = (RetConst)authResponse.BaseResponse.Ret;
			Log.smethod_5("Network", "auth parsed success. ");
			if (uin != 0 && (ret == RetConst.MM_OK || ret == RetConst.MM_ERR_CRITICALUPDATE || ret == RetConst.MM_ERR_RECOMMENDEDUPDATE))
			{
				Log.smethod_4("Network", "auth PASS, uin= " + uin);
				AuthRequest authRequest = sessionPack.mRequestObject as AuthRequest;
				if (authRequest != null && authRequest.UserName.String == "facebook@wechat_auth")
				{
					SessionPackMgr.getAccount().setAuthInfo(authResponse.UserName.String, authResponse.Password, authResponse.Password);
				}
				SessionPackMgr.setSessionKey(authResponse.SessionKey.ToByteArray());
				SessionPackMgr.getAccount().setUin(uin);
				SessionPackMgr.setAuthStatus(2);
				return authResponse;
			}
			Log.smethod_1("Network", "auth Failed,ret = " + ret);
			if (ret == RetConst.MM_ERR_NEEDREG)
			{
				Log.smethod_4("Network", "auth result: need register");
				SessionPackMgr.mAuthTicket1 = authResponse.Ticket;
				Connector.close();
				return authResponse;
			}
			SessionPackMgr.setSessionKey(null);
			SessionPackMgr.getAccount().reset();
			AccountMgr.reSetBackgroundAccout();
			RetConst retConst = ret;
			if (retConst != RetConst.MM_ERR_AUTH_ANOTHERPLACE)
			{
				switch (retConst)
				{
				case RetConst.MM_ERR_RECOMMENDEDUPDATE:
					Log.smethod_1("Network", "Auth Failed: MM_ERR_RECOMMENDEDUPDATE ");
					break;
				case RetConst.MM_ERR_CRITICALUPDATE:
					Log.smethod_1("Network", "Auth Failed: MM_ERR_CRITICALUPDATE ");
					break;
				default:
					switch (retConst)
					{
					case RetConst.MM_ERR_NEED_VERIFY:
						Log.smethod_1("Network", "Auth Failed: MM_ERR_NEED_VERIFY ");
						break;
					case RetConst.MM_ERR_NOUSER:
						Log.smethod_1("Network", "Auth Failed: MM_ERR_NOUSER ");
						break;
					case RetConst.MM_ERR_PASSWORD:
						Log.smethod_1("Network", "Auth Failed: MM_ERR_PASSWORD ");
						break;
					}
					break;
				}
			}
			else
			{
				Log.smethod_1("Network", "Auth Failed: MM_ERR_AUTH_ANOTHERPLACE ");
				Sender.getInstance().closeSender();
			}
			Connector.close();
			return authResponse;
		}
		public static bool preProcessAuthPack(SessionPack sessionPack, ref object responseObject)
		{
			int mCmdID = sessionPack.mCmdID;
			if (mCmdID == 1)
			{
				responseObject = AuthPack.onParserAuthPack(sessionPack);
				return responseObject != null;
			}
			return true;
		}
	}
}
